package com.wie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wie.constant.MessageConstant;
import com.wie.domain.dto.LoginDTO;
import com.wie.domain.entity.Login;
import com.wie.domain.vo.LoginVO;
import com.wie.exception.AccountNotFoundException;
import com.wie.exception.PasswordErrorException;
import com.wie.service.LoginService;
import com.wie.mapper.LoginMapper;
import com.wie.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author WiedersehenM
* @description 针对表【login(用户登录表)】的数据库操作Service实现
* @createDate 2024-10-22 22:35:16
*/
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login>
    implements LoginService{

    @Autowired
    private LoginMapper loginMapper;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    public Login login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        // 对密码进行md5加密
        String loginPassword = MD5Utils.encode(password);
        //根据用户名查询数据库中的数
        List<Login> loginList = lambdaQuery()
                .eq(Login::getUsername, username)
                .list();
        if (loginList == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        Login loginUser = loginList.get(0);
        //校验用户
        if (!loginPassword.equals(loginUser.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_WRONG);
        }

        return loginUser;
    }
}




